<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门诊号(挂号号)" prop="registrationId">
        <el-input
          v-model="queryParams.registrationId"
          placeholder="请输入门诊号(挂号号)"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初诊结果str串" prop="priliminaryDiseStrList">
        <el-input
          v-model="queryParams.priliminaryDiseStrList"
          placeholder="请输入初诊结果str串"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发病时间" prop="startDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择发病时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="病人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入病人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病人性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入病人性别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病人年龄" prop="ageStr">
        <el-input
          v-model="queryParams.ageStr"
          placeholder="请输入病人年龄"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查部位字符串列表" prop="checkStrList">
        <el-input
          v-model="queryParams.checkStrList"
          placeholder="请输入检查部位字符串列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处置字符串列表" prop="dispositionStrList">
        <el-input
          v-model="queryParams.dispositionStrList"
          placeholder="请输入处置字符串列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="草药处方列表" prop="herbalPrescriptionStrList">
        <el-input
          v-model="queryParams.herbalPrescriptionStrList"
          placeholder="请输入草药处方列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="确诊诊断串" prop="definiteDiseStrList">
        <el-input
          v-model="queryParams.definiteDiseStrList"
          placeholder="请输入确诊诊断串"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病人id" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入病人id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验结果字符串" prop="testStrList">
        <el-input
          v-model="queryParams.testStrList"
          placeholder="请输入检验结果字符串"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="西药处方" prop="medicinePrescriptionStrList">
        <el-input
          v-model="queryParams.medicinePrescriptionStrList"
          placeholder="请输入西药处方"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="初诊疾病id列表" prop="priliminaryDiseIdList">
        <el-input
          v-model="queryParams.priliminaryDiseIdList"
          placeholder="请输入初诊疾病id列表"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查结果" prop="checkResult">
        <el-input
          v-model="queryParams.checkResult"
          placeholder="请输入检查结果"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验结果" prop="testResult">
        <el-input
          v-model="queryParams.testResult"
          placeholder="请输入检验结果"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dms:case_history:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dms:case_history:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dms:case_history:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dms:case_history:export']"
        >导出</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="case_historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="主诉" align="center" prop="chiefComplaint" />
      <el-table-column label="现病史" align="center" prop="historyOfPresentIllness" />
      <el-table-column label="现治疗情况" align="center" prop="historyOfTreatment" />
      <el-table-column label="既往史" align="center" prop="pastHistory" />
      <el-table-column label="过敏史" align="center" prop="allergies" />
      <el-table-column label="体格检查" align="center" prop="healthCheckup" />
      <el-table-column label="门诊号(挂号号)" align="center" prop="registrationId" />
      <el-table-column label="初诊结果str串" align="center" prop="priliminaryDiseStrList" />
      <el-table-column label="发病时间" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="病人姓名" align="center" prop="name" />
      <el-table-column label="病人性别" align="center" prop="gender" />
      <el-table-column label="病人年龄" align="center" prop="ageStr" />
      <el-table-column label="检查部位字符串列表" align="center" prop="checkStrList" />
      <el-table-column label="处置字符串列表" align="center" prop="dispositionStrList" />
      <el-table-column label="草药处方列表" align="center" prop="herbalPrescriptionStrList" />
      <el-table-column label="确诊诊断串" align="center" prop="definiteDiseStrList" />
      <el-table-column label="病人id" align="center" prop="patientId" />
      <el-table-column label="检验结果字符串" align="center" prop="testStrList" />
      <el-table-column label="西药处方" align="center" prop="medicinePrescriptionStrList" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="初诊疾病id列表" align="center" prop="priliminaryDiseIdList" />
      <el-table-column label="检查结果" align="center" prop="checkResult" />
      <el-table-column label="检验结果" align="center" prop="testResult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dms:case_history:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:case_history:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改病历对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主诉" prop="chiefComplaint">
          <el-input v-model="form.chiefComplaint" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="现病史" prop="historyOfPresentIllness">
          <el-input v-model="form.historyOfPresentIllness" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="现治疗情况" prop="historyOfTreatment">
          <el-input v-model="form.historyOfTreatment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="既往史" prop="pastHistory">
          <el-input v-model="form.pastHistory" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="过敏史" prop="allergies">
          <el-input v-model="form.allergies" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="体格检查" prop="healthCheckup">
          <el-input v-model="form.healthCheckup" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="门诊号(挂号号)" prop="registrationId">
          <el-input v-model="form.registrationId" placeholder="请输入门诊号(挂号号)" />
        </el-form-item>
        <el-form-item label="初诊结果str串" prop="priliminaryDiseStrList">
          <el-input v-model="form.priliminaryDiseStrList" placeholder="请输入初诊结果str串" />
        </el-form-item>
        <el-form-item label="发病时间" prop="startDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发病时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="病人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入病人姓名" />
        </el-form-item>
        <el-form-item label="病人性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入病人性别" />
        </el-form-item>
        <el-form-item label="病人年龄" prop="ageStr">
          <el-input v-model="form.ageStr" placeholder="请输入病人年龄" />
        </el-form-item>
        <el-form-item label="检查部位字符串列表" prop="checkStrList">
          <el-input v-model="form.checkStrList" placeholder="请输入检查部位字符串列表" />
        </el-form-item>
        <el-form-item label="处置字符串列表" prop="dispositionStrList">
          <el-input v-model="form.dispositionStrList" placeholder="请输入处置字符串列表" />
        </el-form-item>
        <el-form-item label="草药处方列表" prop="herbalPrescriptionStrList">
          <el-input v-model="form.herbalPrescriptionStrList" placeholder="请输入草药处方列表" />
        </el-form-item>
        <el-form-item label="确诊诊断串" prop="definiteDiseStrList">
          <el-input v-model="form.definiteDiseStrList" placeholder="请输入确诊诊断串" />
        </el-form-item>
        <el-form-item label="病人id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入病人id" />
        </el-form-item>
        <el-form-item label="检验结果字符串" prop="testStrList">
          <el-input v-model="form.testStrList" placeholder="请输入检验结果字符串" />
        </el-form-item>
        <el-form-item label="西药处方" prop="medicinePrescriptionStrList">
          <el-input v-model="form.medicinePrescriptionStrList" placeholder="请输入西药处方" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="初诊疾病id列表" prop="priliminaryDiseIdList">
          <el-input v-model="form.priliminaryDiseIdList" placeholder="请输入初诊疾病id列表" />
        </el-form-item>
        <el-form-item label="检查结果" prop="checkResult">
          <el-input v-model="form.checkResult" placeholder="请输入检查结果" />
        </el-form-item>
        <el-form-item label="检验结果" prop="testResult">
          <el-input v-model="form.testResult" placeholder="请输入检验结果" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCase_history, getCase_history, delCase_history, addCase_history, updateCase_history, exportCase_history } from "@/api/dms/case_history";

export default {
  name: "Case_history",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 病历表格数据
      case_historyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chiefComplaint: null,
        historyOfPresentIllness: null,
        historyOfTreatment: null,
        pastHistory: null,
        allergies: null,
        healthCheckup: null,
        registrationId: null,
        priliminaryDiseStrList: null,
        startDate: null,
        name: null,
        gender: null,
        ageStr: null,
        checkStrList: null,
        dispositionStrList: null,
        herbalPrescriptionStrList: null,
        definiteDiseStrList: null,
        patientId: null,
        testStrList: null,
        medicinePrescriptionStrList: null,
        status: null,
        priliminaryDiseIdList: null,
        checkResult: null,
        testResult: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sms_registration_rank").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询病历列表 */
    getList() {
      this.loading = true;
      listCase_history(this.queryParams).then(response => {
        this.case_historyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chiefComplaint: null,
        historyOfPresentIllness: null,
        historyOfTreatment: null,
        pastHistory: null,
        allergies: null,
        healthCheckup: null,
        registrationId: null,
        priliminaryDiseStrList: null,
        startDate: null,
        name: null,
        gender: null,
        ageStr: null,
        checkStrList: null,
        dispositionStrList: null,
        herbalPrescriptionStrList: null,
        createTime: null,
        definiteDiseStrList: null,
        patientId: null,
        testStrList: null,
        medicinePrescriptionStrList: null,
        status: null,
        priliminaryDiseIdList: null,
        checkResult: null,
        testResult: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加病历";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCase_history(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改病历";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCase_history(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCase_history(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除病历编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCase_history(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有病历数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCase_history(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
